﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace ykglxt
{
    public partial class Form_Login : Form
    {
        public string sIsAdmin;
        private string sBM="";
        public Form_Login()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                labelWarning.Visible = false;
                SqlConnection con = new SqlConnection(PublicUnit.ConnetionString);
                con.Open();
                SqlCommand com = new SqlCommand("SELECT * FROM Dic_RY WHERE BM = @BM AND PWD = @PWD", con);
                com.Parameters.Add("@BM", SqlDbType.VarChar, 10);
                com.Parameters.Add("@PWD", SqlDbType.VarChar, 10);
                com.Parameters["@BM"].Value = sBM;
                com.Parameters["@PWD"].Value = tbxPassword.Text;
                SqlDataReader dr = com.ExecuteReader();
                if (dr.Read())
                {
                    PublicUnit.UserID = dr["BM"].ToString().Trim();
                    PublicUnit.UserName = dr["MC"].ToString().Trim();
                    PublicUnit.UserKSBM = dr["KSBM"].ToString().Trim();
                    sIsAdmin = dr["IsAdmin"].ToString();
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    labelWarning.Text = "帐号或密码错误，请重新输入！";
                    labelWarning.Visible = true;
                }
                dr.Close();
                con.Close();
            }
            catch (SqlException)
            {
                MessageBox.Show("网络故障，请稍候再试！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void Form_Login_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                e.Handled = true;
                SendKeys.Send("{TAB}");
            }
        }

        private void tbxUser_TextChanged(object sender, EventArgs e)
        {
            labelWarning.Visible = false;
        }

        private void tbxPassword_TextChanged(object sender, EventArgs e)
        {
            labelWarning.Visible = false;
        }

        private void tbxUser_Validated(object sender, EventArgs e)
        {
            tbxUser.Text = FindInfo(tbxUser.Text);
        }

        private string FindInfo(string sInfo)
        {
            sBM = "";
            string sSQL;
            if (Regex.IsMatch(tbxUser.Text, "[0-9]{1,10}"))
            {
                sSQL = "SELECT * FROM Dic_RY WHERE BM = @BM";
            }
            else if (Regex.IsMatch(tbxUser.Text, "^[a-zA-Z]+$"))
            {
                sSQL = "SELECT * FROM Dic_RY WHERE ZJF = @ZJF";
            }
            else
            {
                sSQL = "SELECT * FROM Dic_RY WHERE MC = @MC";
            }
                using (SqlConnection con = new SqlConnection(PublicUnit.ConnetionString))
                {
                    con.Open();
                    SqlCommand com = new SqlCommand(sSQL, con);
                    com.Parameters.Add("@BM", SqlDbType.VarChar);
                    com.Parameters.Add("@MC", SqlDbType.VarChar);
                    com.Parameters.Add("@ZJF", SqlDbType.VarChar);
                    com.Parameters["@BM"].Value = tbxUser.Text;
                    com.Parameters["@MC"].Value = tbxUser.Text;
                    com.Parameters["@ZJF"].Value = tbxUser.Text;
                    SqlDataReader dr = com.ExecuteReader();
                    if (dr.Read())
                    {
                        sBM = dr["BM"].ToString().Trim();
                        if(dr["MC"] != null)
                            return dr["MC"].ToString().Trim();
                    }
                    dr.Close();
                    con.Close();
                }
            return tbxUser.Text;
        }
    }
}
